package com.rem.concurrency.juc.my9Pool;

import cn.hutool.core.thread.ThreadFactoryBuilder;
import org.apache.commons.lang3.concurrent.BasicThreadFactory;

import java.util.concurrent.*;

/**
 * alibaba代码规范建议创建线程池
 *
 * @author Rem
 * @date 2021-03-10
 */

public class Demo03 {

    public static void main(String[] args) {

      //  ExecutorService pool = new Demo3().methord1();
        ExecutorService pool = new Demo03().methord2();
        try {
            for (int i = 0; i < 20; i++) {
                pool.execute(() -> {
                    System.out.println(Thread.currentThread().getName());
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            pool.shutdown();
        }
    }


    ExecutorService methord1() {
        return new ScheduledThreadPoolExecutor(
                3,
                new BasicThreadFactory.Builder().namingPattern("example-schedule-pool-%d").daemon(true).build());
    }

    ExecutorService methord2() {
        return new ThreadPoolExecutor(5,
                200,
                0L,
                TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<>(1024),
                new ThreadFactoryBuilder().build(),
                new ThreadPoolExecutor.AbortPolicy());
    }
}
